package main

import (
	"MPCBot/global"
	"MPCBot/handle"
	"context"
	"log"
	_ "net/http/pprof"
	"os"
	"os/signal"
	"time"

	"github.com/labstack/echo/v4"
)

func main() {

	log.SetFlags(log.Lshortfile | log.LstdFlags)
	log.SetOutput(os.Stdout)

	defer func() {
		if err := recover(); err != nil {
			println("run error：%v", err)
			time.Sleep(1 * time.Minute)
		}
	}()

	inputConfigPath := os.Args[1]
	println("read config file:", inputConfigPath)
	err := global.InitGlobal(&global.ConfPath{ConfigPath: inputConfigPath})
	if err != nil {
		println("Initialization fail：", err.Error())
		time.Sleep(1 * time.Minute)
		return
	}

	e := echo.New()

	// Initialize scheduled task service
	handle.Init()

	quit := make(chan os.Signal)
	signal.Notify(quit, os.Interrupt)
	<-quit
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	if err := e.Shutdown(ctx); err != nil {
		e.Logger.Fatal(err)
	}
}
